package chenji.dailykit.kit;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;

import org.bukkit.inventory.ItemStack;

import chenji.dailykit.Main;
import chenji.dailykit.configuration.ConfigException;
import chenji.dailykit.configuration.KConfiguration;
import chenji.dailykit.kit.kits.CommonKit;
/**
 * @author ChenJi Copyright (c) 2016 ChenJi. All rights reserved.
 */
public class KitCenter extends KConfiguration {
	private final HashMap<String, Class<? extends SimpleKit>> SimpleKitList = new HashMap<String, Class<? extends SimpleKit>>();
	private HashMap<String, SimpleKit> KitList = new HashMap<String, SimpleKit>();

	public KitCenter() throws ConfigException {
		super("Kits.yml", true);
		try {
			this.saveConfig();
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.loadConfig();
	} 

	public void loadDefaultKit() {
		
		for (SimpleKit k : this.loadKitsFromConfiguration("CommonKit")) {
			this.KitList.put(k.getKitName(), k);
			
		}
		StringBuilder s = new StringBuilder();
		s.append("共加载 ");
		s.append(this.KitList.size());
		s.append(" 个礼包");
		Main.getLog().info(s.toString());

	}

	private HashSet<SimpleKit> loadKitsFromConfiguration(String Name) {
		
		Set<String> s = this.getConfig().getKeys(false);
		HashSet<SimpleKit> kits = new HashSet<SimpleKit>();
		for (String ss : s) {
			if (this.getConfig().contains(ss + ".Type")) {
				
				String sa = this.getConfig().getString(ss + ".Type");
				
				if (!(sa.equalsIgnoreCase(Name))) {
					continue;
				}
				Class<? extends SimpleKit> clazz = null;
				if (sa.equalsIgnoreCase("CommonKit")) {
					clazz = CommonKit.class;
				} else {
					if (this.SimpleKitList.containsKey(Name)) {
						clazz = this.SimpleKitList.get(Name);
					} else {
					
						continue;
					}
				}

				try {
					SimpleKit k = this.loadKitFromConfiguration(ss, sa, clazz);
					if (k == null) {
						StringBuilder ms = new StringBuilder();
						ms.append("礼包：");
						ms.append(ss);
						ms.append(" 加载失败。");
						Main.getLog().log(Level.WARNING, ms.toString());
					}
					kits.add(k);
				} catch (KitCannotLoadException e) {
					e.printStackTrace();
					StringBuilder ms = new StringBuilder();
					ms.append("礼包：");
					ms.append(ss);
					ms.append(" 加载失败。");
					Main.getLog().log(Level.WARNING, ms.toString());
				}
			}
		}
		return kits;
	}

	private SimpleKit loadKitFromConfiguration(String Path, String Type, Class<? extends SimpleKit> clazz)
			throws KitCannotLoadException {
	
		SimpleKit kit = null;
		try {
			kit = clazz.newInstance();
		} catch (InstantiationException e) {
			StringBuilder b = new StringBuilder();
			b.append("loadKitFromConfiguration方法创建对象失败，路径");
			b.append(Path);
			b.append("，类型：");
			b.append(Type);
			throw new KitCannotLoadException(b.toString());
		} catch (IllegalAccessException e) {
			StringBuilder b = new StringBuilder();
			b.append("loadKitFromConfiguration拒绝访问，路径");
			b.append(Path);
			b.append("，类型：");
			b.append(Type);
			throw new KitCannotLoadException(b.toString());
		}

		ItemStack i;
		String ShowItemPath = Path + ".ShowItem";
		i = this.getItemStack(ShowItemPath, Path);
		if (i == null) {
			return null;
		} else {
			kit.setShowItem(i);
		}

		String ShowinMenuItem = Path + ".ShowinMenuItem";
		i = this.getItemStack(ShowinMenuItem, Path);
		if (i == null) {
			return null;
		} else {
			kit.setShowinMenuItem(i);
		}
		String Permission = this.getString(Path + ".Permission", Path);
		kit.setPermission(Permission);
		kit.setKitName(Path);

		List<ItemStack> itemlist = getItemStackList(Path + ".ItemList", Path);
		if (itemlist == null) {
			return null;
		} else {
			HashSet<ItemStack> ia = new HashSet<ItemStack>();
			ia.addAll(itemlist);
			kit.setItemList(ia);
		}
		if (!(kit.loadSettingsPath(this.getConfig(), Path))) {
			return null;
		}
		return kit;

	}

	private List<ItemStack> getItemStackList(String Path, String KitName) {
		if (!(this.getConfig().contains(Path))) {
			StringBuilder s = new StringBuilder();
			s.append("加载礼包");
			s.append("时发生异常：未找到");
			s.append(Path);
			s.append("节点。无法加载物品列表。请勿手动修改Kits.yml，请使用/DailyKit Editor 进行礼包编辑。");
			Main.getLog().log(Level.WARNING, s.toString());
			return null;
		}
		List<?> l = this.getConfig().getList(Path);
		if (l.size() == 0) {
			StringBuilder s = new StringBuilder();
			s.append("加载礼包");
			s.append(KitName);
			s.append("时发生异常：");
			s.append(Path);
			s.append("节点是一个空的列表。请勿手动修改Kits.yml，请使用/DailyKit Editor 进行礼包编辑。");
			Main.getLog().log(Level.WARNING, s.toString());
			return null;
		}
		Object o = l.get(0);
		if (!(o instanceof ItemStack)) {
			StringBuilder s = new StringBuilder();
			s.append("加载礼包");
			s.append(KitName);
			s.append("时发生异常：");
			s.append(Path);
			s.append("节点的列表内包含的不是ItemStack。请勿手动修改Kits.yml，请使用/DailyKit Editor 进行礼包编辑。");
			Main.getLog().log(Level.WARNING, s.toString());
		}
		@SuppressWarnings("unchecked")
		List<ItemStack> list = (List<ItemStack>) l;
		return list;

	}

	private ItemStack getItemStack(String Path, String KitName) {
		if (this.getConfig().contains(Path)) {
			ItemStack i = this.getConfig().getItemStack(Path);
			if (i != null) {
				return i;
			} else {
				StringBuilder s = new StringBuilder();
				s.append("加载礼包");
				s.append(KitName);
				s.append("时发生异常：");
				s.append(Path);
				s.append("节点是一个无效的ItemStack。请勿手动修改Kits.yml，请使用/DailyKit Editor 进行礼包编辑。");
				Main.getLog().log(Level.WARNING, s.toString());
				return null;
			}
		} else {
			StringBuilder s = new StringBuilder();
			s.append("加载礼包");
			s.append("时发生异常：未找到");
			s.append(Path);
			s.append("节点。请勿手动修改Kits.yml，请使用/DailyKit Editor 进行礼包编辑。");
			Main.getLog().log(Level.WARNING, s.toString());
			return null;
		}
	}

	private String getString(String Path, String KitName) {
		if ((!this.getConfig().contains(Path)) || this.getConfig().getString(Path) == null) {
			StringBuilder s = new StringBuilder();
			s.append("加载礼包");
			s.append("时发生异常：未找到");
			s.append(Path);
			s.append("节点。请勿手动修改Kits.yml，请使用/DailyKit Editor 进行礼包编辑。");
			Main.getLog().log(Level.WARNING, s.toString());
			return null;
		}
		return this.getConfig().getString(Path);
	}

	@Override
	public void loadConfig() {
		loadDefaultKit();
	}

	/**
	 * 注册一个Kit模板
	 */
	public void registerKit(Class<? extends SimpleKit> clazz, String Name) {
		this.SimpleKitList.put(Name, clazz);
	}

	/**
	 * 获取识别名对应的SimpleKit对象。
	 * 
	 * @param Name
	 * @return
	 */
	public SimpleKit getSimpleKit(String Name) {
		if (this.KitList.containsKey(Name)) {
			return this.KitList.get(Name);
		}
		return null;
	}

}
